//
//  Toast.swift
//  ShareVipForBusiness
//
//  Created by zw on 2017/10/11.
//  Copyright © 2017年 zev. All rights reserved.
//

import UIKit
import SnapKit
enum ToastType {
    case ToastTypeSuccess
    case ToastTypeFail
}
enum ToastPosition {
    case center
    case bottom
}
fileprivate let showDuration : TimeInterval = 1.5
class Toast : NSObject{
    @objc static func showSuccess(content:String? ,duration:TimeInterval = showDuration) -> Void {
        Toast.show(content: content, type: .ToastTypeSuccess)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
            Toast.dismiss()
        }
    }
    
    @objc static func showFail(content:String?, duration:TimeInterval = showDuration) -> Void {
        Toast.show(content: content, type: .ToastTypeFail)
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
            Toast.dismiss()
        }
    }
    
    static func showInfo(content:String?, duration:TimeInterval = showDuration,postion:ToastPosition = .bottom) -> Void {
        Toast.dismiss(anim: false)
        let window : UIWindow? = UIApplication.shared.delegate?.window as? UIWindow
        let view = UIView()
        view.frame = CGRect.init(x: 0, y: 0, width: ScreenUtils.getWidth(), height: ScreenUtils.getHeight())
        view.tag = 998
        view.backgroundColor = UIColor.clear
        view.alpha = 0.0;
        window!.addSubview(view)
        
        let bgView = UIView.init(frame: CGRect.zero)
        bgView.backgroundColor = UIColor.withHex(hexString: "333333");
        bgView.center = view.center;
        bgView.layer.masksToBounds = true
        bgView.layer.cornerRadius = 4.0
        view.addSubview(bgView)
//        bgView.snp.makeConstraints { (make) in
//            make.center.equalTo(view.center)
//        }
        
        if postion == .bottom{
            bgView.snp.makeConstraints { (make) in
                make.centerX.equalTo(view.snp.centerX)
                make.bottom.equalTo(view.snp.bottom).offset(-150)
            }
        }else{
            bgView.snp.makeConstraints { (make) in
                make.centerX.equalTo(view.snp.centerX)
                make.centerY.equalToSuperview()
            }
        }
        
        let contentLabel = UILabel()
        contentLabel.textColor = UIColor.white
        contentLabel.font = UIFont.systemFont(ofSize: 14)
        contentLabel.text = content
        contentLabel.lineBreakMode = .byWordWrapping
        contentLabel.textAlignment = .center
        contentLabel.numberOfLines = 0
        bgView.addSubview(contentLabel)
        
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bgView).offset(5)
            make.right.equalTo(bgView).offset(-5)
            make.top.equalTo(bgView).offset(5)
            make.bottom.equalTo(bgView).offset(-5)
            make.width.lessThanOrEqualTo(200)
            make.width.greaterThanOrEqualTo(80)
        }
        
        UIView.animate(withDuration: 0.2) {
            view.alpha = 1.0;
        }
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
            Toast.dismiss()
        }
    }
    
    static func show(content:String?, type:ToastType) -> Void {
        Toast.dismiss(anim: false)
        let window : UIWindow? = UIApplication.shared.delegate?.window as? UIWindow
        let view = UIView()
        view.frame = CGRect.init(x: 0, y: 0, width: ScreenUtils.getWidth(), height: ScreenUtils.getHeight())
        view.tag = 998
        view.backgroundColor = UIColor.clear
        view.alpha = 0.0;
        window!.addSubview(view)
        
        let contentView = UIView()
        contentView.backgroundColor = UIColor.withHex(hexString: "dfdfdf",alpha: 0.98)
        contentView.layer.cornerRadius = 8.0
        contentView.layer.shadowColor = UIColor.black.cgColor
        contentView.layer.shadowRadius = 3
        contentView.layer.shadowOffset = CGSize.init(width: 0, height: 3)
        contentView.layer.shadowOpacity = 0.3
        view.addSubview(contentView)
        
        let typeImageView = UIImageView.init(frame: CGRect.init(x: 36, y: 20, width: 48, height: 48))
        typeImageView.contentMode = .center
        switch type {
        case .ToastTypeSuccess:
            typeImageView.image = UIImage.init(named: "success_icon")
        case .ToastTypeFail:
            typeImageView.image = UIImage.init(named: "fail_icon")
        }
        contentView.addSubview(typeImageView)
        
        let contentLabel = UILabel()
        contentLabel.textColor = deepBlackColor
        contentLabel.font = UIFont.systemFont(ofSize: 16)
        contentLabel.text = content
        contentLabel.frame = CGRect.init(x: 0, y: 78, width: 120, height: 22)
        contentLabel.numberOfLines = 1
        contentLabel.lineBreakMode = .byWordWrapping
        contentLabel.textAlignment = .center
        contentView.addSubview(contentLabel)
        
        contentView.frame = CGRect.init(x: 0, y: 0, width: 120, height: 120)
        contentView.center = view.center
        UIView.animate(withDuration: 0.2) {
            view.alpha = 1.0;
        }
    }
    
    static func dismiss(anim:Bool = true) -> Void {
        let window : UIWindow? = UIApplication.shared.delegate?.window as? UIWindow
        let view = window?.viewWithTag(998)
        if anim{
            UIView.animate(withDuration: 0.1, animations: {
                view?.alpha = 0.0
            }) { (Bool) in
                view?.removeFromSuperview()
            }
        }else{
            view?.removeFromSuperview()
        }
        
    }
}
